package com.next.servlet;

import com.bean.UpdatePws;
import com.bean.User;
import com.bean.UserPage;
import com.dao.NextHistoryDao;
import com.dao.UserDao;
import com.utils.MD5Util;
import com.utils.PageUtil;
import com.utils.ResponseUtils;
import com.utils.StringUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName UserServlet
 * @Desc 后台用户控制层
 * @Author xiaoding
 * @Date 2021/1/16 20:10
 * @Version 1.0
 */
@WebServlet(name = "NextUserServlet",value = "/NextUserServlet")
public class UserServlet extends HttpServlet {
    private UserDao userDao = new UserDao();
    private NextHistoryDao nextHistoryDao = new NextHistoryDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能模块
        String str = request.getParameter("flag");

        //判断功能模块
        if (str.equals("select")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //查询所有用户的数量
            int count = userDao.userCount();
            //定义访问的Servlet
            String targetUrl = "NextUserServlet";
            //定义参数
            String param = "flag=select";
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<User> users = userDao.userSelect(new UserPage(page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("users",users);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/user.jsp").forward(request, response);
        }else if (str.equals("search")) {
            //获取图书名称
            String uNames = request.getParameter("uNames");
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //根据用户名称，查询用户数量
            int count = userDao.userSearchCount("%"+uNames+"%");
            //定义访问的Servlet
            String targetUrl = "NextUserServlet";
            //定义参数
            String param = "flag=search&uNames="+uNames;
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<User> users = userDao.userSearch(new UserPage("%"+uNames+"%",page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("users",users);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/user.jsp").forward(request, response);
        }else if (str.equals("selectId")) {
            //获取用户Id
            int uId = Integer.parseInt(request.getParameter("uId"));

            //设置状态、路径
            String message = null;
            String url = null;

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置搜索名称
            String uNames = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("uNames"))) {
                uNames = request.getParameter("uNames");
            }

            //根据Id查询用户
            User users = userDao.userSelectId(uId);

            //设置会话
            request.setAttribute("users",users);
            //跳转方法
            request.getRequestDispatcher("next/updateUser.jsp?page="+page+"&uNames="+uNames).forward(request,response);
        }else if (str.equals("add")) {
            //获取参数
            String account = request.getParameter("account");
            String pws = MD5Util.MD5(request.getParameter("pws"));
            String uName = request.getParameter("uName");
            String email = request.getParameter("email");
            int count = Integer.parseInt(request.getParameter("count"));
            int age = Integer.parseInt(request.getParameter("age"));
            String sex = request.getParameter("sex");

            //设置路径、状态
            String message = null;
            String url = null;

            //调用添加用户的方法
            int a = userDao.addUser(new User(account,pws,uName,email,count,age,sex));

            //判断是否添加成功
            if (a > 0) {
                message = "添加成功";
            }else {
                message = "添加失败";
            }

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置搜索名称
            String uNames = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("uNames"))) {
                uNames = request.getParameter("uNames");
                url = "NextUserServlet?flag=search&page="+page+"&uNames="+uNames;
            }else {
                url = "NextUserServlet?flag=select&page="+page;
            }

            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("update")) {
            //获取参数
            int uId = Integer.parseInt(request.getParameter("uId"));
            String account = request.getParameter("account");
            String pws = MD5Util.MD5(request.getParameter("pws"));
            String uName =  request.getParameter("uName");
            String email = request.getParameter("email");
            int count = Integer.parseInt(request.getParameter("count"));
            int age = Integer.parseInt(request.getParameter("age"));
            String sex = request.getParameter("sex");

            //设置状态、路径
            String message = null;
            String url = null;

            //调用修改信息的方法
            int a = userDao.updateUser(new User(uId,account,pws,uName,email,count,age,sex));

            //判断是否修改成功
            if (a > 0) {
                message = "内容修改成功";
            }else {
                message = "内容修改失败";
            }

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置搜索名称
            String uNames = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("uNames"))) {
                uNames = request.getParameter("uNames");
                url = "NextUserServlet?flag=search&page="+page+"&uNames="+uNames;
            }else {
                url = "NextUserServlet?flag=select&page="+page;
            }
            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("updateMy")) {
            //获取参数
            int uId = Integer.parseInt(request.getParameter("uId"));
            String account = request.getParameter("account");
            String pws = MD5Util.MD5(request.getParameter("pws"));
            String uName =  request.getParameter("uName");
            String email = request.getParameter("email");
            int count = Integer.parseInt(request.getParameter("count"));
            int age = Integer.parseInt(request.getParameter("age"));
            String sex = request.getParameter("sex");

            //设置状态、路径
            String message = null;
            String url = null;

            //调用修改信息的方法
            int a = userDao.updateUser(new User(uId,account,pws,uName,email,count,age,sex));

            //判断是否修改成功
            if (a > 0) {
                message = "内容修改成功";
                url = "NextBookServlet?flag=select";
            }else {
                message = "内容修改失败";
                url = "NextBookServlet?flag=select";
            }

            //根据ID,获取用户信息
            User user = userDao.userSelectId(uId);
            //设置加密前的密码
            user.setPws(request.getParameter("pws"));
            //设置会话
            request.getSession().setAttribute("user",user);

            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("updatePws")) {
            //获取参数
            int uId = Integer.parseInt(request.getParameter("uId"));
            String oldPws = MD5Util.MD5(request.getParameter("oldPws"));
            String newPws = MD5Util.MD5(request.getParameter("newPws"));
            //设置状态、路径
            String message = null;
            String url = null;

            //调用修改密码的方法
            int a = userDao.updatePws(new UpdatePws(uId,oldPws,newPws));

            //判断是否修改成功
            if (a > 0) {
                message = "密码修改成功";
                url = "NextLoginServlet?flag=exit";
            }else {
                message = "密码修改失败";
                url = "NextBookServlet?flag=select";
            }
            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("delete")) {
            //获取用户Id
            int uId = Integer.parseInt(request.getParameter("uId"));

            //设置状态、路径
            String message = null;
            String url = null;

            //调用删除的方法
            int a = nextHistoryDao.deleteUserIdHistory(uId);
            int b = userDao.deleteUser(uId);
            //判断是否删除成功
            if (a > 0 && b > 0) {
                message = "删除成功";
            }else {
                message = "删除失败";
            }

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置搜索名称
            String uNames = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("uNames"))) {
                uNames = request.getParameter("uNames");
                url = "NextUserServlet?flag=search&page="+page+"&uNames="+uNames;
            }else {
                url = "NextUserServlet?flag=select&page="+page;
            }
            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("selectJSON")) {
            //获取数据
            List<User> list = userDao.userSelects();
            //将数据转换为JSON格式
            JSONObject obj = new JSONObject();
            obj.put("data", JSONArray.fromObject(list));
            //发送数据
            ResponseUtils.writeHTML(response,obj.toString());
        }else if (str.equals("isRepetitive")) {
            //获取数据
            List<User> list = userDao.isRepetitive();
            //将数据转换为JSON格式
            JSONObject obj = new JSONObject();
            obj.put("data", JSONArray.fromObject(list));
            //发送数据
            ResponseUtils.writeHTML(response,obj.toString());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
